﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

//Class for showing text on screen at over a certain timespan.
namespace TrenchcoatRobots.Classes.ComponentClasses.UI {
	class TextEffect : Component {
		public SpriteFont TextFont { get; set; }
		protected String message;
		protected float timeLeft;
		protected int textStyle;
		protected Color textColor;
		protected int textAngle;
		protected float textScale;
		protected Vector2 textPos;

		protected float lerpValue;
		protected bool decreaseLerp;
		protected float animDuration;

		public float lerpMult;
		public List<Color> TextColors;

		protected void Init() {
			message = "";
			timeLeft = 0;
			textStyle = 0;
			textColor = Color.White;
			TextColors = new List<Color>();
			lerpMult = 10f;
			textAngle = 0;
			textScale = 1;
			lerpValue = 0;
			textPos = new Vector2();
			decreaseLerp = false;
		}

		public TextEffect(ParentMethod gameObjectMethod, SpriteFont font)
			: base(gameObjectMethod, "TextEffect", false) {
			Init();
			TextFont = font;
			TextColors.Add(Color.White);
			TextColors.Add(Color.Red);
			TextColors.Add(Color.Yellow);
		}

		public virtual void Activate(int textType, float duration, string text) {
			timeLeft = duration;
			animDuration = duration;
			message = text;
			textStyle = textType;
		}

		public override void Update(ref GameTime gameTime) {
			if (timeLeft > 0) {
				if (lerpValue < 0) {
					decreaseLerp = false;
				}
				else if (lerpValue > 1) {
					decreaseLerp = true;
				}
				if (decreaseLerp) {
					lerpValue -= (float)gameTime.ElapsedGameTime.TotalSeconds * lerpMult;
				}
				else {
					lerpValue += (float)gameTime.ElapsedGameTime.TotalSeconds * lerpMult;
				}

				timeLeft -= (float)gameTime.ElapsedGameTime.TotalSeconds;

				if (textStyle == 0) {
					textColor = TextColors[0];
					textAngle = 0;
					textScale = 1;
				}
				if (textStyle == 1) {
					textColor = Color.Lerp(TextColors[1], TextColors[2], lerpValue);
					textAngle = 0;
					textScale = 1;
				}
				if (textStyle == 2) {
					textPos = new Vector2(textPos.X, (float)Math.Sin(gameTime.TotalGameTime.TotalSeconds * 10) * 10);
					textColor = TextColors[1];
					textAngle = (int)Math.Floor((float)Math.Sin(gameTime.TotalGameTime.TotalSeconds * 25) * 10) - 5;
					textScale = (float)MathHelper.SmoothStep(0.75f, 1.25f, (float)Math.Sin(lerpValue)) * (MathHelper.Min(.25f, (timeLeft / animDuration)) * 4);
				}
			}
		}

		public override void Draw(ref SpriteBatch spriteBatch, ref Datatypes.DrawInfo drawinfo) {
			if (timeLeft > 0) {
				spriteBatch.DrawString(TextFont, message, base.Parent().Position + textPos, textColor, MathHelper.ToRadians(textAngle), TextFont.MeasureString(message) * 0.5f, textScale, SpriteEffects.None, 0);
				//spriteBatch.Draw(drawinfo.blankTexture, new Rectangle((int)Math.Floor(base.Parent().Position.X + textPos.X), (int)Math.Floor(base.Parent().Position.Y + textPos.Y), 5, 5), Color.Gray);
			}
		}
	}
}
